<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Car;
use App\Models\Brand;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CarController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Car::with(['brand']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('brand.title', NOT_LANG_BRAND);
            $grid->column('title');
            $grid->column('type')->using(admin_trans('car.options.type'));
            $grid->column('status')->using(admin_trans('car.options.status'));
            $grid->column('pics')->display(function ($pics) {
                $pics = json_decode($pics, true);
                return $pics[0];
            })->gallery();
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('title');
                $brand = Brand::all()->pluck('title', 'id');
                $filter->equal('brand_id')->select($brand);
                $filter->equal('type')->radio(carType());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, Car::with(['brand']), function (Show $show) {
            $show->field('id');
            $show->field('brand.title', admin_trans('car.fields.brand_id'));
            $show->field('title');
            $show->field('type')->using(admin_trans('car.options.type'));
            $show->field('pics')->image();
            $show->field('status')->using(admin_trans('car.options.status'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Car(), function (Form $form) {
            $form->display('id');
            $form->select('brand_id')
                ->options(Brand::where('status', DEFAULT_STATUS_SUCCESS)
                ->pluck('title', 'id'))
                ->required();
            $form->text('title')->required();
            $form->radio('type')->options(admin_trans('car.options.type'))->default('1');
            $form->radio('status')->options(admin_trans('car.options.status'))->default('1');
            $form->multipleImage('pics')->uniqueName()->saveAsJson()->required()->help('可多图上传');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
